import {
	baseUrl,
	api,
	apioss,
	appid
} from './config.js';

const fail = (res) => {
	setTimeout(() => {
		uni.showToast({
			title: '连接服务器错误',
			icon: 'none'
		})
	}, 500)
}
//跳转登录
export const goLogin = () => {
	const urlPath = `${baseUrl}/#/pages/autoLogin/autoLogin`
	const url =
		`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(urlPath)}&response_type=code&scope=snsapi_userinfo&state=appId=${appid}`
	window.location = url
}

const REQ_CONFIG = (path, params, promiseObj) => {

	return {
		timeout: 10000,
		url: `${api}${path}`,
		data: params,
		header: {
			'Authorization': 'Bearer ' + uni.getStorageSync('token'),
		},
		success: (res) => {
			const {
				code
			} = res.data
			if (res.statusCode >= 400) {
				fail(res)
				promiseObj.reject(res)
				return
			}
			if ((!params?.noLogin && !uni.getStorageSync('token')) || code === 401) {
				// uni.clearStorageSync()
				// uni.showToast({
				// 	title: '请登录',
				// 	icon: 'none'
				// })
				setTimeout(() => {
					goLogin()
				}, 200)
				promiseObj.reject(res)
				return
			}
			if (code === 403) {
				setTimeout(() => {
					uni.showToast({
						title: res.data?.msg ?? '服务器错误',
						icon: 'none'
					})
				}, 200)
				uni.hideLoading()
				promiseObj.reject(res)
			}
			if (code === 500) {
				setTimeout(() => {
					uni.showToast({
						title: res.data?.msg ?? '服务器错误',
						icon: 'none'
					})
				}, 200)
				uni.hideLoading()
				promiseObj.reject(res)
			}
			if (code === 200) {
				uni.hideLoading()
				promiseObj.resolve(res)
			}
		},
		fail: (res) => {
			fail(res)
			promiseObj.reject(res)
		}
	}
}
class Http {
	api = api
	apioss = apioss
	//文件上传封装
	uploadFile(url) {
		console.log(url)
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: `${this.api}/system/oss/upload`,
				filePath: url,
				name: 'file',
				header: {
					'Authorization': 'Bearer ' + uni.getStorageSync('token'),
				},
				formData: {
					'bucket': 'file'
				},
				success: (uploadFileRes) => {
					if (uploadFileRes.data && JSON.parse(uploadFileRes.data)) {
						const data = JSON.parse(uploadFileRes.data)
						if (data.code === 200) {
							resolve(data.data)
							return;
						}
						if (data.code === 401) {
							uni.clearStorageSync()
							uni.hideLoading()
							uni.showToast({
								title: '请登录',
								icon: 'none'
							})
							setTimeout(() => {
								goLogin()
							}, 500)
							reject(data)
						}
						uni.hideLoading()
						reject(data)
					} else {
						uni.hideLoading()
						reject(data)
					}
					resolve();
				},
				fail(e) {
					reject(e)
				}
			});
		})
	}
	//图片选择上传封装
	async uploadImage(count = 1) {
		const chooseImage = (count) => {
			return new Promise((resolve, reject) => {
				uni.chooseImage({
					count,
					success(chooseImageRes) {
						resolve(chooseImageRes.tempFilePaths)
					},
					fail(e) {
						reject(e)
					}
				})
			})
		}
		const url = await chooseImage(count);
		const uploadArr = []
		uni.showLoading()
		for (let i of url) {
			uploadArr.push(this.uploadFile(i))
		}
		const arr = await Promise.all(uploadArr)
		uni.hideLoading()
		return arr;
	}
	get(path, params) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: "GET",
				...REQ_CONFIG(path, params, {
					resolve,
					reject
				}),

			})
		})
	}
	put(path, params) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: "PUT",
				...REQ_CONFIG(path, params, {
					resolve,
					reject
				}),
			})
		})
	}
	post(path, params) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: "POST",
				...REQ_CONFIG(path, params, {
					resolve,
					reject
				}),
			})
		})
	}
	deletes(path, params) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: "DELETE",
				...REQ_CONFIG(path, params, {
					resolve,
					reject
				}),
			})
		})
	}
}
export default new Http()